package cn.lj.user.service.impl;

import cn.lj.base.Exception.BusinessException;
import cn.lj.base.service.impl.BaseServiceImpl;
import cn.lj.base.util.Constant;
import cn.lj.base.util.MD5Utils;
import cn.lj.base.util.StrUtils;
import cn.lj.user.domain.LoginInfo;
import cn.lj.user.domain.User;
import cn.lj.user.dto.UserDto;
import cn.lj.user.mapper.LoginInfoMapper;
import cn.lj.user.mapper.UserMapper;
import cn.lj.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTempla;
    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    @Transactional
    public void reginster(UserDto userDto) {
        /*登录业务分析*/
        /*if(userDro.type = 1 :用户手机号注册)*/
        /*校验：电话，验证码，密码，验证密码不能为空*/
        if (userDto.getType() == 1) {/*手机验证码注册*/
            if (userDto.getPhone() == null
                    || userDto.getPassword() == null
                    || userDto.getVerifyCode() == null
                    || userDto.getConfirmPwd() == null) {
                throw new BusinessException("请输入完整注册信息");
            }
            /*校验：两次密码是否一致*/
            if (!userDto.getPassword().equals(userDto.getConfirmPwd())) {
                throw new BusinessException("两次密码不一致");
            }
            /*检查：验证码是够过期*/
            String codeTemp = (String)redisTempla.opsForValue().get(Constant.REGISTER + ":" + userDto.getPhone());
            /*！！！codeTemp需要拆分*/
            String[] split = codeTemp.split(":");
            String s = split[0];
            if (s == null) {
                throw new BusinessException("验证码已经过期");
            }
            /*对比：输入的验证码与redis查询的是否一致*/
            if (!userDto.getVerifyCode().equals(s)) {
                throw new BusinessException("验证码输入错误");
            }
            /*保存：1.先保存登录到logininfo表单
             * 2.userDto初始化logininfo*/
            LoginInfo loginfo = userDto2LoginInfo(userDto);
            /*如何初始化：1.将用户的用户名与电话都存为手机号*/
            /* logininfoMapper.save(); 保存logininfo，生成主键*/
            loginInfoMapper.save(loginfo);
            /*拷贝logininfo到user*/
            User user = info2User(loginfo);
            user.setLogininfo_id(loginfo.getId());
            /*添加用户*/
            userMapper.save(user);
        }
    }
    /*拷贝logininfo到user*/
    /*Logininfo:username,phone,email,salt,password,type,disable[0：未启用，1：启用]*/
    /*User:     username,email,phone,salt,password,state,age,createtime,headImg,logininfo_id*/
    private User info2User(LoginInfo loginInfo){
        User user = new User();
        /*拷贝同名属性*/
        BeanUtils.copyProperties(loginInfo,user);
        /*设置不同名的属性*/
        user.setState(1);
        return user;
    }
    /*id,username,phone,email,salt,password,type,disable[0：未启用，1：启用]*/
    /*把userDto转为logininfo对象*/
    private LoginInfo userDto2LoginInfo(UserDto userDto){
        LoginInfo loginInfo = new LoginInfo();
        /*把用户名设置为手机号*/
        loginInfo.setUsername(userDto.getPhone());
        loginInfo.setPhone(userDto.getPhone());
        /*设置用户类型：0：用户，1：管理员*/
        loginInfo.setType(0);
        /*获取盐值*/
        String salt = StrUtils.getComplexRandomString(32);
        /*设置盐值*/
        loginInfo.setSalt(salt);
        /*生成加密密码*/
        String password = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        /*设置加密密码*/
        loginInfo.setPassword(password);
        /*设置状态*/
        loginInfo.setDisable(1);
        return loginInfo;
    }

}
